package zut.cs.liziflower.controller;


import org.springframework.web.bind.annotation.*;
import zut.cs.liziflower.entity.Comments;
import zut.cs.liziflower.entity.Result;
import zut.cs.liziflower.service.impl.CommentsServiceImpl;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xuwenke
 * @since 2022-05-19
 */
@RestController
@RequestMapping("/api/comments")
public class CommentsController {

    @Resource
    CommentsServiceImpl commentsService;

    @GetMapping("/get-all-comments")
    public Result<?> getCommentByUserIdAndFlowerId(@RequestParam("userid") Integer userid, @RequestParam("flowerid") Integer flowerid) {
        List<Comments> list = commentsService
                .getCommentByUserIdAndFlowerId(userid, flowerid);
        if (list != null) {
            return Result.success(list);
        }
        return Result.error("404", "暂无评论", null);
    }

    @GetMapping("/get-comment")
    public Result<?> getComment(@RequestParam("flowerid") Integer flowerid) {
        List<Comments> list = commentsService
                .getComment(flowerid);
        if (list != null) {
            return Result.success(list);
        }
        return Result.error("404", "暂无评论", null);
    }

    @PostMapping("/add-remark")
    public Result<?> addRemark(@RequestBody Comments comments) {
//        @RequestParam("userid") Integer userid,
//        @RequestParam("remark") String remark,
//        @RequestParam("flowerid") Integer flowerid
        int flag = commentsService.addRemark(comments.getUserid(),
                comments.getFlowerid(), comments.getRemark());
        if (flag == 1) {
            return Result.success("评论成功");
        } else {
            return Result.error("405", "评价失败", null);
        }


    }

}
